<template>
	<xn-form-container
		title="设置线索推送公海"
		:width="600"
		:visible="visible"
		:destroy-on-close="true"
		:body-style="{ 'padding-top': '0px' }"
		@close="onClose"
	>
		<a-form ref="formRef" :model="formData" :rules="formRules" class="p-10">
			<a-form-item label="大有线索" name="highSeasId">
				<a-select
					v-model:value="formData.highSeasId"
					:options="highSeasOptions"
					:field-names="{
						label: 'name',
						value: 'id'
					}"
					placeholder="请选择"
				/>
			</a-form-item>
		</a-form>
		<template #footer>
			<div class="flex items-center justify-end">
				<a-space>
					<a-button type="primary" :loading="submitLoading" @click="onSubmit">确定</a-button>
					<a-button style="margin-right: 8px" @click="onClose">取消</a-button>
				</a-space>
			</div>
		</template>
	</xn-form-container>
</template>

<script setup>
	import { required } from '@/utils/formRules'
	import clueHighSeasApi from '@/api/biz/crm/clueHighSeasApi'
	import customerHighSeasApi from '@/api/biz/crm/customerHighSeasApi'

	const submitLoading = ref(false)

	// 默认是关闭状态
	const visible = ref(false)
	const formRef = ref()
	const emit = defineEmits({ successful: null })

	// 表单数据
	const formData = ref({})

	const highSeasOptions = ref([])

	// 打开抽屉
	const onOpen = () => {
		visible.value = true
		clueHighSeasApi.list().then((data) => {
			const target = data.find((item) => item.isPushData)
			if (target) {
				formData.value.highSeasId = target.id
			}
			highSeasOptions.value = data
		})
	}
	// 关闭抽屉
	const onClose = () => {
		visible.value = false
		formRef.value.resetFields()
	}

	// 默认要校验的
	const formRules = {
		highSeasId: [required('请选择')]
	}
	// 验证并提交数据
	const onSubmit = () => {
		formRef.value.validate().then(() => {
			submitLoading.value = true
			customerHighSeasApi
				.cluePush({ id: formData.value.highSeasId })
				.then(() => {
					onClose()
					emit('successful')
				})
				.finally(() => {
					submitLoading.value = false
				})
		})
	}

	// 调用这个函数将子组件的一些数据和方法暴露出去
	defineExpose({
		onOpen
	})
</script>

<style scoped type="less"></style>
